<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 钉钉机器人Markdown消息 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace ding\msg\robot;

/**
 * 给机器人发送的Markdown消息
 *
 * @package ding\msg\robot
 */
class MarkdownRobotMsg extends RobotMsg {
    protected $title;
    protected $markdownText;
    protected $atMobiles;
    protected $isAtAll;

    /**
     * markdown消息构造函数
     *
     * @param $title
     * @param $markdownText
     * @param array $atMobiles
     * @param bool $isAtAll
     */
    public function __construct($title, $markdownText, $atMobiles = [], $isAtAll = false) {
        parent::__construct("markdown");
        $this->title = $title;
        $this->markdownText = $markdownText;
        $this->atMobiles = $atMobiles;
        $this->isAtAll = $isAtAll;
    }

    /**
     * 转换为可发送的消息
     * {
     *        "msgtype": "markdown",
     *        "markdown": {
     *           "title":"杭州天气",
     *           "text": "#### 杭州天气 @156xxxx8827\n" +
     *                   "> 9度，西北风1级，空气良89，相对温度73%\n\n" +
     *                   "> ![screenshot](http://image.jpg)\n"  +
     *                   "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"
     *        },
     *        "at": {
     *            "atMobiles": [
     *                "156xxxx8827",
     *                "189xxxx8325"
     *            ],
     *            "isAtAll": false
     *        }
     * }
     * @return array
     */
    public function toMsg() {
        return [
            'msgtype' => $this->msgtype,
            'markdown' => [
                'title' => $this->title,
                'text' => $this->markdownText
            ],
            'at' => [
                'atMobiles' => $this->atMobiles,
                'isAtAll' => $this->isAtAll
            ]
        ];
    }
}